<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var gradeDatagrid;
	var gradeStudentDatagrid;
	$(function() {
		gradeDatagrid = $('#student_gradeList_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/gradeAction!queryGrade.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20 ],
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			striped : true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 80,
				checkbox : true
			}, {
				field : 'studentName',
				title : '姓名',
				width : 80,
				align : 'left'
			}, {
				field : 'studentNo',
				title : '学号',
				width : 80,
				align : 'left'
			}, {
				field : 'courseName',
				title : '课程名称',
				width : 100,
				align : 'left'
			}, {
				field : 'examScore',
				title : '考试成绩',
				width : 100,
				align : 'right'
			}, {
				field : 'homeworkScore',
				title : '作业成绩',
				width : 100,
				align : 'right'
			}, {
				field : 'attendanceScore',
				title : '出勤成绩',
				width : 100,
				align : 'right'
			}, {
				field : 'overallGrade',
				title : '综合成绩',
				width : 100,
				align : 'right'
			}, {
				field : 'memo',
				title : '备注',
				width : 150,
				align : 'left'
			} ] ],
			toolbar : [ {
				text : '查询',
				iconCls : 'icon-search',
				handler : function() {
					queryGrade();
				}
			}, '-', {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					addGrade();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					editGrade();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					removeGrade();
				}
			}, '-', {
				text : '导入',
				iconCls : 'icon-down',
				handler : function() {
					importGrade();
				}
			}, '-', {
				text : '模板下载',
				iconCls : 'icon-up',
				handler : function() {
					downloadTemplate();
				}
			} ]
		});
		
		$('.datagrid-header div').css({
			'textAlign' : 'center'
		});

		function queryGrade() {
			$('#student_gradeList_queryForm input').val('');
			$('#student_gradeList_queryDialog').dialog('open');
		}

		function addGrade() {
			$('#student_gradeAdd_addGradeForm input').val('');
			var dlg = $('<div style="padding:10px 30px;" />').dialog({
				width : 420,
				height : 380,
				href : '${pageContext.request.contextPath}/pages/student/gradeAdd.jsp',
				modal : true,
				title : '课程新增',
				buttons : [ {
					text : '保存',
					handler : function() {
						$('#student_gradeAdd_addGradeForm').form('submit', {
							url : '${pageContext.request.contextPath}/gradeAction!addGrade.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									gradeDatagrid.datagrid('insertRow', {
										index : 0,
										row : obj.obj
									});
									dlg.dialog('close');
								}
								$.messager.show({
									title : '提示',
									msg : obj.msg
								});
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						$('#student_gradeAdd_addGradeForm input').val('');
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				},
				onLoad : function() {
					$('#student_gradeAdd_courseCombobox').combobox('reload', '${pageContext.request.contextPath}/courseAction!queryCourseList.action');
				}
			});
		}

		function editGrade() {
			var rows = gradeDatagrid.datagrid('getChecked');
			if (rows.length == 1) {
				var dlg = $('<div style="padding:10px 30px;" />').dialog({
					width : 420,
					height : 380,
					href : '${pageContext.request.contextPath}/pages/student/gradeEdit.jsp',
					modal : true,
					title : '课程编辑',
					buttons : [ {
						text : '保存',
						handler : function() {
							$('#student_gradeEdit_editGradeForm').form('submit', {
								url : '${pageContext.request.contextPath}/gradeAction!editGrade.action',
								success : function(r) {
									var obj = jQuery.parseJSON(r);
									if (obj.success) {
										dlg.dialog('close');
										gradeDatagrid.datagrid('updateRow', {
											index : gradeDatagrid.datagrid('getRowIndex', rows[0].id),
											row : obj.obj
										});
									}
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								}
							});
						}
					}, {
						text : '取消',
						handler : function() {
							dlg.dialog('close');
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						$('#student_gradeEdit_editGradeForm').form('load', rows[0]);
						$('#student_gradeEdit_editGradeForm input[name=id]').val(rows[0].id);
						$('#student_gradeEdit_courseCombobox').combobox('reload', '${pageContext.request.contextPath}/courseAction!queryCourseList.action');
						$('#student_gradeEdit_courseCombobox').combobox('select', rows[0].courseId);
					}
				});
			} else {
				$.messager.alert('提示', '一次只能修改一条数据！');
			}
		}

		function removeGrade() {
			var rows = gradeDatagrid.datagrid('getChecked');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('确认', '确定要删除选择的数据吗？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/gradeAction!removeGrade.action',
							data : {
								ids : ids.join(',')
							},
							dataType : 'json',
							success : function(r) {
								gradeDatagrid.datagrid('load');
								gradeDatagrid.datagrid('unselectAll');
								$.messager.show({
									title : '提示',
									msg : r.msg,
								});
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！');
			}
		}
		
		function importGrade() {
			var dlg = $('<div style="padding:20px 30px;" />').dialog({
				width : 400,
				height : 200,
				href : '${pageContext.request.contextPath}/pages/student/gradeImport.jsp',
				modal : true,
				title : '成绩导入',
				buttons : [ {
					text : '导入',
					handler : function() {
						$('#student_gradeImport_importGradeForm').form('submit', {
							url : '${pageContext.request.contextPath}/gradeAction!importGrade.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									dlg.dialog('close');
									gradeDatagrid.datagrid('load');
									gradeDatagrid.datagrid('uncheckAll');
									gradeDatagrid.datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								} else {
									$.messager.alert('提示', obj.msg);
								}
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				}
			});
		}

		function downloadTemplate() {
			$.messager.confirm('确认', '确定要下载导入模板吗？', function(r) {
				if (r) {
					window.location.href = '${pageContext.request.contextPath}/gradeAction!downloadTemplate.action';
					gradeDatagrid.datagrid('uncheckAll');
					gradeDatagrid.datagrid('unselectAll');
				}
			});
		}
		

	});
	
	function openStudentDialog() {
		$('#student_gradeList_studentDialog').dialog('open');
		gradeStudentDatagrid = $('#student_gradeList_studentDatagrid').datagrid({
			url : '${pageContext.request.contextPath}/studentAction!queryStudent.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			striped : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10 ],
			sortName : 'studentName',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 50,
				checkbox : true
			}, {
				field : 'studentName',
				title : '姓名',
				width : 100,
				sortable : true
			}, {
				field : 'studentNo',
				title : '学号',
				width : 100,
				sortable : true
			}, {
				field : 'title',
				title : '职称',
				width : 100,
				align : 'center',
				hidden : true
			}, {
				field : 'corporationName',
				title : '工作单位',
				width : 150,
				align : 'center'
			}, {
				field : 'position',
				title : '职务',
				width : 100,
				align : 'center'
			}, {
				field : 'mobile',
				title : '手机',
				width : 100,
				align : 'center'
			}, {
				field : 'QQ',
				title : 'qq',
				width : 100,
				align : 'center'
			}, {
				field : 'email',
				title : '电子邮件',
				width : 100,
				align : 'center'
			} ] ],
			toolbar : '#student_gradeList_studentToolbar',
			onDblClickRow : function(rowIndex, rowData) {
				$('#student_gradeAdd_addGradeForm input[name=studentName]').val(rowData.studentName);
				$('#student_gradeAdd_addGradeForm input[name=studentNo]').val(rowData.studentNo);
				$('#student_gradeAdd_addGradeForm input[name=studentId]').val(rowData.id);
				$('#student_gradeList_studentDialog').dialog('close');
			}
		});
		gradeStudentDatagrid.datagrid('unselectAll');
		gradeStudentDatagrid.datagrid('uncheckAll');
	}

	function queryStudent() {
		gradeStudentDatagrid.datagrid('load', serializeObject($('#student_gradeList_queryStudentForm')));
	}
</script>
<div id="student_courseList_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false">
		<table id="student_gradeList_datagrid"></table>
	</div>
</div>
<div id="student_gradeList_queryDialog" class="easyui-dialog" data-options="title:'成绩查询',closed:true,modal:true,
			buttons:[{
				text : '查询',
				handler : function(){
					$('#student_gradeList_datagrid').datagrid('load', serializeObject($('#student_gradeList_queryForm')));
				    $('#student_gradeList_queryDialog').dialog('close');
				    $('#student_gradeList_datagrid').datagrid('uncheckAll');
				    $('#student_gradeList_datagrid').datagrid('unselectAll');
				}
			},{
				text : '清空',
				handler : function(){
					$('#student_gradeList_queryForm input').val('');
				}
			}]" style="width:380px;height:200px;padding:20px 20px;" align="center">
	<form id="student_gradeList_queryForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">姓名：</td>
				<td><input name="studentName" style="width: 240px;"/></td>
			</tr>
			<tr>
				<td style="text-align: right;">学号：</td>
				<td><input name="studentNo" style="width: 240px;"/></td>
			</tr>
			<tr>
				<td style="text-align: right;">课程名称：</td>
				<td><input name="courseName" style="width: 240px;" /></td>
			</tr>
		</table>
	</form>
</div>
<div id="student_gradeList_studentDialog" class="easyui-dialog" data-options="title:'学生查询',closed:true,modal:true" style="width:800px;height:500px;">
	<div id="student_gradeList_studentLayout" class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'center',border:false">
			<table id="student_gradeList_studentDatagrid"></table>
		</div>
	</div>
</div>
<div id="student_gradeList_studentToolbar" style="height:60px;padding:5px 0 0 20px;">
	<form id="student_gradeList_queryStudentForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">姓名：</td>
				<td><input name="studentName" style="width:200px;" /></td>
			</tr>
			<tr>
				<td style="text-align: right;">学号：</td>
				<td><input name="studentNo" style="width:200px;" />&nbsp;&nbsp; <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="queryStudent()">查询</a></td>
			</tr>
		</table>
	</form>
</div>